﻿@{
    ViewBag.Title = "Code";
}

<h2>Code</h2>

<link href="@Url.Content("~/Content/prettify.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/prettify.js")" type="text/javascript"></script>

<p class='wow' id='playground'>The code is available from <a href='http://cosby.codeplex.com/SourceControl/list/changesets'>Cosby.CodePlex.com</a></p>
<p>Sadly, there is very little in the code that I'm proud of. But there are two ideas that I'm excited about, so I'll give some snippets here:</p>

<h2>FileResult</h2>
<p>The entire project is just a way to try out returning files as a result of an action (rather than return html or json, the two most common results).</p>

<pre class="prettyprint">
//  'widthColors' parameter is 'width' then 'color' over and over,
//     as many as you want, separated by '/'s.
public FileResult Stripes(string orientation, string widthcolors)
{
    //turn an 'orientation' and 'widthColors' into a 'stripe set' object 
    var ss = new StripeSet(orientation, widthcolors);

    var fullyQualifiedPath = ss.GetFileName();

    if (!System.IO.File.Exists(fullyQualifiedPath))
    {
        //Create the image (uses GDI+) and store it on disk
        ss.SaveImage();
    }

    //return the stored image
    return base.File(fullyQualifiedPath, "image/png");
}
</pre>

<script type="text/javascript">
    $(document).ready(function () {
        prettyPrint();
    });
</script>

<h2>Custom Routes</h2>

<p>The other thing it made me look for was a way of having custom routes with an unlimited number of slashes.</p>
<p>I didn't find a particularly sexy way to achieve this, and here's the custom route I was left with:</p>
<pre class="prettyprint">
routes.MapRoute("Stripes", "Image/Stripes/{orientation}/{*widthcolors}",
        new { controller = "Image", orientation = "V", action = "Stripes"});
</pre>
<p>So that way when you get a url such as:<br />
<pre>  /Image/Stripes/V/50/FFF/2/FEE/1/000/2/FFE</pre>
...everything after the 'orientation' (i.e. V) is treated as a single parameter. So:</p>
<pre>  widthColors = "50/FFF/2/FEE/1/000/2/FFE"</pre></p>
<p>I say it's 'unsexy' because you're then left with the job of parsing that url fragment yourself and turning it into whatever structure suits. And parsing a url fragment yourself and turning it into whatever structure suits is, by definition, a non-libidinous activity.</p>
<p>And that's all I think is worth mentioning about the code.</p>
<h2>Tests?</h2>
<p>None. I'm going to coder hell already, I may as well skip the queue.</p>



